<template>
  <div class="com-container">
    <div class="com-chart" ref="hot_ref"></div>
    <span class="iconfont arr_left" @click="toLeft" :style="comStyle"
      >&#xe6ef;</span
    >
    <span class="iconfont arr_right" @click="toRight" :style="comStyle"
      >&#xe6ed;</span
    >
    <span class="cat_name" :style="comStyle">{{ catTitle }}</span>
  </div>
</template> 
<script>
import { mapState } from "vuex";
import { getThemeValue } from "@/utils/theme_utils";
export default {
  data() {
    return {
      chartInstance: null,
      allData: null,
      // 增加 currentIndex 索引代表当前显示的数据索引, 后期通过左右箭头改变 currentIndex 的值
      currentIndex: 0,
      titleFontSize: 0,
    };
  },
  created () {
    // 在组件创建完成之后 进行回调函数的注册
    this.$socket.registerCallBack('hotData', this.getData)
  },
  mounted() {
    this.initChart();
    // this.getData();
    this.$socket.send({
      action: 'getData',
      socketType: 'hotData',
      chartName: 'hotproduct',
      value: ''
    })
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    // 在组件销毁的时候, 进行回调函数的取消
    this.$socket.unRegisterCallBack('hotData')
  },
  computed: {
    catTitle() {
      if (!this.allData) {
        return "";
      }
      return this.allData[this.currentIndex].name;
    },
    comStyle() {
      return {
        fontSize: this.titleFontSize + "px",
        color: getThemeValue(this.theme).titleColor,
      };
    },
    ...mapState(['theme'])
  },
  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.hot_ref, this.theme);
      const initOption = {
        title: { text: "▎ 热销商品销售金额占比统计", left: 20, top: 20 },
        legend: { top: "15%", icon: "circle" },
        series: [
          {
            type: "pie",
            label: {
              // 隐藏文字
              show: false,
            },
            labelLine: {
              // 隐藏线
              show: false,
            },
            emphasis: {
              label: {
                // 高亮显示文字
                show: true,
              },
            },
          },
        ],
        tooltip: {
          trigger: "item",
          formatter: function (params) {
            // console.log(params);
            let tipArray = [];
            params.data.children.forEach(function (item) {
              let childStr = ` ${item.name}&nbsp;&nbsp;&nbsp; ${
                parseInt((item.value / params.value) * 100) + "%"
              } `;
              tipArray.push(childStr);
            });
            return tipArray.join("<br/>");
          },
        },
      };
      this.chartInstance.setOption(initOption);
    },
    async getData(res) {
      // 获取服务器的数据, 对this.allData进行赋值之后, 调用updateChart方法更新图表
      // const { data: res } = await this.$http.get("hotproduct");
      this.allData = res;
      // console.log(res);
      this.updateChart();
    },
    updateChart() {
      // 处理图表需要的数据
      // 饼图数据
      const seriesData = this.allData[this.currentIndex].children.map(
        (item) => {
          return {
            value: item.value,
            name: item.name,
            children: item.children,
          };
        }
      );
      // 图例数据
      const legendData = this.allData[this.currentIndex].children.map(
        (item) => {
          return item.name;
        }
      );
      const dataOption = {
        legend: { data: legendData },
        series: [{ data: seriesData }],
      };
      this.chartInstance.setOption(dataOption);
    },
    screenAdapter() {
      this.titleFontSize = (this.$refs.hot_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        title: { textStyle: { fontSize: this.titleFontSize } },
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize / 2,
          textStyle: { fontSize: this.titleFontSize / 2 },
        },
        series: [{ radius: this.titleFontSize * 4.5, center: ["50%", "60%"] }],
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
    toLeft() {
      this.currentIndex--;
      if (this.currentIndex < 0) {
        this.currentIndex = this.allData.length - 1;
      }
      this.updateChart();
    },
    toRight() {
      this.currentIndex++;
      if (this.currentIndex > this.allData.length - 1) {
        this.currentIndex = 0;
      }
      this.updateChart();
    },
  },
  watch: {
    theme () {
      this.chartInstance.dispose() // 销毁当前的图表
      this.initChart() // 重新以最新的主题名称初始化图表对象
      this.screenAdapter() // 完成屏幕的适配
      this.updateChart() // 更新图表的展示
    }
  }
};
</script> 

<style lang="less" scoped>
.arr_left {
  position: absolute;
  left: 10%;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  color: white;
}
.arr_right {
  position: absolute;
  right: 10%;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  color: white;
}
.cat_name {
  position: absolute;
  left: 80%;
  bottom: 20px;
  font-weight: bold;
  color: white;
}
</style>